package org.jb.coinche.client.business.handler.impl;

import org.jb.coinche.client.business.message.AcceptCoincheMessage;
import org.jb.coinche.client.business.message.SurCoincheMessage;
import org.jb.coinche.client.business.model.Auction;
import org.jb.coinche.client.business.model.CoincheAuction;
import org.jb.coinche.client.business.model.Player;
import org.jb.coinche.client.gui.card.AuctionPanel;
import org.jb.coinche.client.gui.card.CardList;
import org.jb.coinche.client.gui.card.CardPanel;
import org.jb.coinche.client.gui.card.SetPanel;
import org.jb.coinche.client.gui.card.StatusPanel;
import org.jb.coinche.client.guistatemachine.GUIStateHandler;
import org.jb.coinche.client.guistatemachine.MessageSender;
import org.jb.coinche.client.user.UserLogin;

import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.google.inject.Inject;

public class GUICoincheHandler implements GUIStateHandler<CoincheAuction> {

	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.guistatemachine.GUIStateHandler#disable()
	 */
	@Override
	public void disable() {
		if (msgBox != null) {
			msgBox.close();
			msgBox = null;
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.guistatemachine.GUIStateHandler#enable()
	 */
	@Override
	public void enable() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.guistatemachine.GUIStateHandler#getStateClass()
	 */
	@Override
	public Class<CoincheAuction> getStateClass() {
		return CoincheAuction.class;
	}

	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.guistatemachine.GUIStateHandler#update(org.jb.coinche.client.guistatemachine.State)
	 */
	@Override
	public void update(CoincheAuction state) {

		/*
		 * Carte
		 */
		Player self = state.getGame().getPlayer( user );
		if (self != null) {
			cardList.setCardList( self.getCards() );
		}
		cardList.setClickable(false);
		
		Player p = self;
		if (p == null)
			p = state.getGame().getNextPlayer(null);
		
		for( CardPanel cp : setPanel.getCardPanels() ) {
			
			if (state.getAuction().getPlayer().equals(p)) {
				cp.setWaiting( p.getName() );
			} else {
				Auction a = state.getPlayerAuction(p);
				
				if (a == null) {
					cp.setParameters(p.getName(), "");
				} else {
					if (a.getValue() == 0) {
						cp.setParameters( p.getName(), "Passe");
					} else {
						cp.setParameters( p.getName(), a.getValue() + " " + a.getTrump().toText() );
					}
				}
			}
			
			p = state.getGame().getNextPlayer(p);
		}
		
		auctionPanel.setCurrentAuction(state.getAuction());
		if (state.getAuction().getPlayer().equals(self)) {
			statusPanel.setStatus("<span style=\"color: red; font-weight: bold\">Vous avez été coinché par "+ state.getCoincheUser() + " !</span>");
			
			if (msgBox == null) {
				msgBox = MessageBox.confirm("Coinché!", "Votre enchère " + state.getAuction().getValue() + " " + state.getAuction().getTrump().toText() + " a été coinché par " + state.getCoincheUser() + "<br>Voulez-vous surcoincher ?", new Listener<MessageBoxEvent>() {
					
					@Override
					public void handleEvent(MessageBoxEvent be) {
						Button btn = be.getButtonClicked();
						if (btn != null) {
							if (Dialog.YES.equals( btn.getItemId() ) ) {
								SurCoincheMessage msg = new SurCoincheMessage();
								sender.send( msg );
							} else {
								AcceptCoincheMessage msg = new AcceptCoincheMessage();
								sender.send( msg );
							}
						}
					}
				});
			}
		} else {
			statusPanel.setStatus( state.getAuction().getPlayer().getName() + " a été coinché par " + state.getCoincheUser() + " !" );
		}
	}
	
	@Inject
	private MessageSender sender;
	
	@Inject
	private CardList cardList;
	
	@Inject
	private SetPanel setPanel;
	
	@Inject
	private AuctionPanel auctionPanel;

	@Inject
	private StatusPanel statusPanel;
	
	@Inject @UserLogin
	private String user;
	
	private MessageBox msgBox = null;

}
